package mt.dummy;

import static org.junit.Assert.*;
import mt.Order;
import mt.comm.ServerSideMessage.Type;

import org.junit.Test;

public class MicroTraderServerDummyTest {
	
	MicroTraderServerDummy mtsd;
	ServerCommDummy sc = new ServerCommDummy();

	@Test(timeout = 5000)
	public void testStart() {
		mtsd = new MicroTraderServerDummy();
		assertNotNull(mtsd);
		assertNotNull(sc);
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 300, 20)));

					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 300, 20)));
				
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 300, 20)));
					
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.CONNECTED, "teste2", null));
					sc.maps.addEntry("test2", null); 
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 200, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 100, 20)));
					
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 500, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 200, 20)));

					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "AA", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "AA", 300, 20)));
					
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 400, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 100, 20)));

					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 200, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 100, 20)));

					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 400, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 100, 20)));
				

					sc.q.add(new ServerMessage(Type.CONNECTED, "Andre", null));
					Thread.sleep(1000);
					sc.maps.addEntry("Andre", null); 
					mtsd.stop();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();
		mtsd.start(sc);
		
	
	
	}

	@Test
	public void testDisconnectClient() {
		mtsd = new MicroTraderServerDummy();
		assertNotNull(mtsd);
		assertNotNull(sc);
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				
				try {
					Thread.sleep(3000);
					sc.q.add(new ServerMessage(Type.CONNECTED, "Andre", null));

					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 300, 20)));
					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createSellOrder("Andre", "Int", 300, 20)));					sc.q.add(new ServerMessage(Type.NEW_ORDER, "Andre", Order.createBuyOrder("Andre", "Int", 300, 20)));

			
					sc.q.add(new ServerMessage(Type.DISCONNECTED, "Andre", null));
					Thread.sleep(1000);
					mtsd.stop();
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();
		mtsd.start(sc);
		
		 
	}
 
}
